package com.hfi.restful.web.register;

import com.hfi.restful.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ChangLiang
 * @date 2019/8/16
 */
@RestController
public class CustomUserController {

    @Autowired
    private CustomUserService customUserService;

    @PostMapping(value = "register")
    public CustomUser registerCustomUser(@RequestParam String mobile,
                                       @RequestParam(required = false) String password) {
        CustomUser customUser = new CustomUser();
        customUser.setMobile(mobile);
        customUser.setPasswd(Utils.generateDefaultPassword());
        return customUserService.registerCustomUser(customUser);
    }

    @PostMapping(value = "setPassword")
    public CustomUser setPassword(@RequestParam(required = false) String mobile,
                                @RequestParam(required = false) String custName,
                                @RequestParam String password) {
        if (!StringUtils.isEmpty(mobile)) {
            CustomUser customUser = new CustomUser();
            customUser.setMobile(mobile);
            customUser.setPasswd(password);
            return customUserService.updateCustomUser(customUser);
        } else if (!StringUtils.isEmpty(custName)) {
            CustomUser customUser = new CustomUser();
            customUser.setCustName(custName);
            customUser.setPasswd(password);
            return customUserService.updateCustomUser(customUser);
        } else {
            throw new RuntimeException("设置密码时未传入mobile或custName");
        }
    }
}
